﻿using System;
using System.Data;
using System.Windows.Forms;
using QuanLyHocSinh.HeThongLuuTru;
using QuanLyHocSinh.HeThongXuLy;


namespace QuanLyThuVien
{
    public partial class FormTiepNhanLopHoc : Form
    {
        LopHocDTO lhHienTai = new LopHocDTO();
 
        public FormTiepNhanLopHoc()
        {
            InitializeComponent();
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            //Khoi tao doi tuong lop hoc
            LopHocDTO lh = new LopHocDTO();
            //doc thong tin cua lop hoc tu form
            lh.TenLop = txtTenLop.Text;
            lh.KhoiLop = int.Parse(txtKhoiLop.Text);
            lh.LopChuyen = txtLopChuyen.Text;
            lh.PhongHoc = txtPhongHoc.Text;
            //goi ham de them du lieu vao csdl
            if (LopHocDAO.ThemLopHoc(lh))
            {
                MessageBox.Show("Them lop hoc thanh cong", "Thong bao");
                HienThiDanhSachLopHoc();
            }
            else
            {
                MessageBox.Show("Them lop hoc that bai", "Thong bao");
            }
        }

        void HienThiDanhSachLopHoc()
        {
            DataTable ds = LopHocDAO.DanhSachLopHoc();
            dgvDanhSachLop.DataSource = ds;

        }

        private void FormTiepNhanLopHoc_Load(object sender, EventArgs e)
        {
            HienThiDanhSachLopHoc();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgvDanhSachLop_SelectionChanged(object sender, EventArgs e)
        {
                if (dgvDanhSachLop.SelectedCells.Count > 0)
                {
                    // lay chi so cua dong dang duoc chon
                    int row = dgvDanhSachLop.SelectedCells[0].RowIndex;
                    // lay lop hoc dc chon tu DGV ra
                    DongToiLopHocHienTai(dgvDanhSachLop.Rows[row]);
                    // hien thi lop hoc len form:
                    txtMaLop.Text = lhHienTai.MaLop.ToString(); 
                    txtTenLop.Text = lhHienTai.TenLop;
                    txtKhoiLop.Text = lhHienTai.KhoiLop.ToString();
                    txtLopChuyen.Text = lhHienTai.LopChuyen;
                    txtPhongHoc.Text = lhHienTai.PhongHoc;
                }
            
        }

        private void DongToiLopHocHienTai(DataGridViewRow row)
        {
            // Cap nhat thong tin cho doi tuong lhHienTai (DTO)



            lhHienTai.MaLop = (int)row.Cells[0].Value;
            lhHienTai.TenLop = (String)row.Cells[1].Value;
            lhHienTai.KhoiLop = (int)row.Cells[2].Value;
            lhHienTai.LopChuyen = (String)row.Cells[3].Value;
            lhHienTai.PhongHoc = (String)row.Cells[4].Value;
            
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            lhHienTai.TenLop = txtTenLop.Text;
            lhHienTai.KhoiLop = int.Parse(txtKhoiLop.Text);
            lhHienTai.LopChuyen = txtLopChuyen.Text;
            lhHienTai.PhongHoc = txtPhongHoc.Text;

            if (LopHocDAO.SuaLopHoc(lhHienTai))
            {
                MessageBox.Show("Sua Lop Hoc Thanh Cong ^_^", "Thong Bao");
                HienThiDanhSachLopHoc();
            }
            else
            {
                MessageBox.Show("Sua Lop Hoc That Bai T_T","Thong Bao");
            }
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (LopHocDAO.XoaLopHoc(lhHienTai))
            {
                MessageBox.Show("Xoa Lop Hoc Thanh Cong ^_^", "Thong Bao");
                HienThiDanhSachLopHoc();
            }
            else {
                MessageBox.Show("Xoa Lop Hoc That Bai T_T", "Thong Bao");
            }
        }

        
                
    }
}
